<!DOCTYPE html>


<html lang="zh-CN">
  

    <head>
      <meta charset="utf-8" />
       
      <meta name="keywords" content="ES6、JS" />
       
      <meta name="description" content="ES6、JS" />
      
      <meta
        name="viewport"
        content="width=device-width, initial-scale=1, maximum-scale=1"
      />
      <title>css-in-js |  君书</title>
  <meta name="generator" content="hexo-theme-ayer">
      
      <link rel="shortcut icon" href="/favicon.ico" />
       
<link rel="stylesheet" href="/dist/main.css">

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css"
      />
      
<link rel="stylesheet" href="/css/custom.css">
 
      <script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>
       
 

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/npm/@sweetalert2/theme-bulma@5.0.1/bulma.min.css"
      />
      <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.19/dist/sweetalert2.min.js"></script>

      <!-- mermaid -->
      
      <style>
        .swal2-styled.swal2-confirm {
          font-size: 1.6rem;
        }
      </style>
    <link rel="alternate" href="/atom.xml" title="君书" type="application/atom+xml">
</head>
  </html>
</html>


<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-css-in-js"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  css-in-js
</h1>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2022/01/11/css-in-js/" class="article-date">
  <time datetime="2022-01-11T06:52:55.000Z" itemprop="datePublished">2022-01-11</time>
</a>   
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">896</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">4 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h1 id="emotion-库学习"><a href="#emotion-库学习" class="headerlink" title="emotion 库学习"></a>emotion 库学习</h1><p>npm 上介绍 <code>emotion 是下一代 css-in-js 的生成器</code>。</p>
<h2 id="css-in-js"><a href="#css-in-js" class="headerlink" title="css-in-js"></a>css-in-js</h2><p>在 web 项目中通过单独引入样式文件为页面上元素添加样式时，css 可能存在以下的局限性：</p>
<ol>
<li>样式在全局作用域内生效，加大了为元素添加独一无二的类名的难度</li>
<li>组件可移植难度加大。移植时除过 js 文件，还要同步移植 css 样式文件</li>
<li>对于动态的样式支持复杂。</li>
</ol>
<p>为了解决这些问题，web 项目中出现了 css-in-js 方案，它是 web 项目中将 css 代码捆绑在 js 中的解决方案，其主要目的：</p>
<ol>
<li>增强代码可移植性</li>
<li>设置样式为组件作用域</li>
<li>方便为组件添加动态样式</li>
</ol>
<p>但由于将 css 揉在了 js 中，不可避免的会出现以下问题：</p>
<ol>
<li>代码的可读性降低</li>
<li>项目复杂度增强</li>
</ol>
<p>所以对于 css-in-js 方案要根据项目合理选择。<code>emotion</code> 是 css-in-js 方案下产生的第三方库，在使用 css-in-js 方案时，可借助 emotion 库，方便快捷的实现需求。</p>
<h2 id="emotion-库"><a href="#emotion-库" class="headerlink" title="emotion 库"></a>emotion 库</h2><p>详细文档可参考 <a target="_blank" rel="noopener" href="https://emotion.sh/docs/introduction">https://emotion.sh/docs/introduction</a>。</p>
<h3 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h3><p><code>There are two primary methods of using Emotion. The first is framework agnostic and the second is for use with React.</code></p>
<p>在项目中使用 emotion 的两种方式：</p>
<ol>
<li>单独使用，不配合框架<ol>
<li><code>@emotion/css</code></li>
</ol>
</li>
<li>配合 react 框架使用<ol>
<li><code>@emotion/react</code></li>
<li>注意 jsx 语法在执行时不能转为默认 <code>React.createElement()</code>，需要转为 <code>jsx()</code> </li>
</ol>
</li>
</ol>
<h3 id="配合-react-框架使用"><a href="#配合-react-框架使用" class="headerlink" title="配合 react 框架使用"></a>配合 react 框架使用</h3><p>emotion 的使用主要分两大类：</p>
<ol>
<li>css 属性 </li>
<li>styled component 借助 <code>@emotion/styled</code> 实现。 <code>styled is a way to create React components that have styles attached to them</code></li>
</ol>
<h4 id="css-属性"><a href="#css-属性" class="headerlink" title="css 属性"></a>css 属性</h4><p>使用 css prop 属性前转换语法的两种方式：</p>
<ol>
<li>babel preset</li>
<li>jsx pragma</li>
</ol>
<p><code>After adding the preset or setting the pragma as a comment, compiled jsx code will use emotion’s jsx function instead of React.createElement.</code></p>
<p>添加 css 属性步骤：</p>
<ol>
<li>设置 babel 或者 jsx pragma</li>
<li>css 可接收 object styles 和 string styles<ol>
<li>object styles 不需要任何导入</li>
<li>string styles 需要使用从 <code>@emotion/react</code> 中导入的 <code>css</code> 函数</li>
</ol>
</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/** <span class="doctag">@jsxRuntime </span>classic */</span></span><br><span class="line"><span class="comment">/** <span class="doctag">@jsx </span>jsx */</span> </span><br><span class="line"><span class="keyword">import</span> &#123; jsx, css &#125; <span class="keyword">from</span> <span class="string">&#x27;@emotion/react&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 对象形式</span></span><br><span class="line"><span class="keyword">const</span> myLabelCss = css(&#123;</span><br><span class="line">  display: <span class="string">&#x27;block&#x27;</span>,</span><br><span class="line">  fontWeight: <span class="string">&#x27;bold&#x27;</span>,</span><br><span class="line">  fontSize: <span class="string">&#x27;14px&#x27;</span></span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">// 字符串形式</span></span><br><span class="line"><span class="keyword">const</span> myInputCss = css`</span><br><span class="line"><span class="css">  <span class="attribute">display</span>: block;</span></span><br><span class="line"><span class="css">  <span class="attribute">border-radius</span>: <span class="number">2px</span>;</span></span><br><span class="line"><span class="css">  <span class="attribute">margin</span>: <span class="number">3px</span> <span class="number">0px</span> <span class="number">5px</span>;</span></span><br><span class="line"><span class="css">`</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">MyTextInput</span>(<span class="params">&#123;label, ...props&#125;</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">const</span> [field, meta] = useField(props);</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;&gt;</span><br><span class="line">      &lt;label css=&#123;myLabelCss&#125; htmlFor=&#123;props.id || props.name&#125;&gt;&#123;label&#125;&lt;/label&gt;</span><br><span class="line">      &lt;input css=&#123;myInputCss&#125; &#123;...field&#125; &#123;...props&#125; /&gt;</span><br><span class="line">    &lt;/&gt;</span><br><span class="line">  )</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><code>myInputCss 和 myLabelCss</code> 的值主要内容如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(myLabelCss);</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;name&quot;</span>: <span class="string">&quot;1bf27r4&quot;</span>,</span><br><span class="line">    <span class="string">&quot;styles&quot;</span>: <span class="string">&quot;display:block;font-weight:bold;font-size:14px;&quot;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(myInputCss);</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;name&quot;</span>: <span class="string">&quot;1hkb5h9&quot;</span>,</span><br><span class="line">    <span class="string">&quot;styles&quot;</span>: <span class="string">&quot;\n  display: block;\n  border-radius: 2px;\n  margin: 3px 0px 5px;\n&quot;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>页面展示效果如图：<br><img src="/2022/01/11/css-in-js/css.png" alt="效果图"></p>
<h4 id="Styled-Components"><a href="#Styled-Components" class="headerlink" title="Styled Components"></a>Styled Components</h4><ol>
<li>引入  <code>@emotion/styled</code></li>
<li>支持以字符串和对象两种形式动态设置样式。具体设置过程如下:</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> styled <span class="keyword">from</span> <span class="string">&#x27;@emotion/styled&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 对象形式动态设置样式</span></span><br><span class="line"><span class="keyword">const</span> Button = styled.button(&#123;</span><br><span class="line">  padding: <span class="string">&#x27;2px 5px 3px&#x27;</span>,</span><br><span class="line">  display: <span class="string">&#x27;block&#x27;</span>,</span><br><span class="line">  color: <span class="string">&#x27;black&#x27;</span>,</span><br><span class="line">&#125;, <span class="function"><span class="params">props</span> =&gt;</span> (&#123;</span><br><span class="line">  color: props.color</span><br><span class="line">&#125;));</span><br><span class="line"><span class="comment">// 动态设置样式2</span></span><br><span class="line"><span class="keyword">const</span> ButtonCopy = styled.button(<span class="function"><span class="params">props</span> =&gt;</span> (&#123;</span><br><span class="line">  padding: <span class="string">&#x27;2px 5px 3px&#x27;</span>,</span><br><span class="line">  display: <span class="string">&#x27;block&#x27;</span>,</span><br><span class="line">  color: props.color || <span class="string">&#x27;black&#x27;</span>,</span><br><span class="line">&#125;));</span><br><span class="line"><span class="comment">// 字符串形式动态设置样式</span></span><br><span class="line"><span class="keyword">const</span> ErrorMsg = styled.div<span class="string">`</span></span><br><span class="line"><span class="string">  color: <span class="subst">$&#123;props =&gt; props.color || <span class="string">&#x27;red&#x27;</span>&#125;</span>;</span></span><br><span class="line"><span class="string">  font-size: 12px;</span></span><br><span class="line"><span class="string">`</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">MyTextInput</span>(<span class="params">&#123;label, ...props&#125;</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">const</span> [field, meta] = useField(props);</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;&gt;</span><br><span class="line">      &lt;label htmlFor=&#123;props.id || props.name&#125;&gt;&#123;label&#125;&lt;/label&gt;</span><br><span class="line">      &lt;input &#123;...field&#125; &#123;...props&#125; /&gt;</span><br><span class="line">      &#123;</span><br><span class="line">        meta.touched &amp;&amp; meta.error </span><br><span class="line">          ? <span class="xml"><span class="tag">&lt;<span class="name">ErrorMsg</span>&gt;</span>&#123;meta.error&#125;<span class="tag">&lt;/<span class="name">ErrorMsg</span>&gt;</span></span></span><br><span class="line">          : <span class="literal">null</span></span><br><span class="line">      &#125;</span><br><span class="line">    &lt;/&gt;</span><br><span class="line">  )</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure> 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>版权声明： </strong>
          
          本博客所有文章除特别声明外，著作权归作者所有。转载请注明出处！
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=https://xiaofujun.gitee.io/2022/01/11/css-in-js/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/css-in-js%E3%80%81emotion/" rel="tag">css-in-js、emotion</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2022/01/18/%E9%A1%B9%E7%9B%AE%E4%B8%AD%E4%B8%B2%E5%B9%B6%E8%A1%8C%E6%89%A7%E8%A1%8C%E5%91%BD%E4%BB%A4%E7%9A%84%E6%96%B9%E5%BC%8F%E6%80%BB%E7%BB%93/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            项目中串并行执行命令的方式总结
          
        </div>
      </a>
    
    
      <a href="/2022/01/10/%E8%BF%9B%E7%A8%8B%E4%B8%8E%E7%BA%BF%E7%A8%8B%E6%95%B4%E7%90%86/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">进程与线程整理</div>
      </a>
    
  </nav>

  
   
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2015-2022
        <i class="ri-heart-fill heart_icon"></i> 小付君
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>    
    </main>
    <div class="float_btns">
      <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

    </div>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="君书"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/img/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/img/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-3.6.0.min.js"></script>
 
<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->
 
<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: ".tocbot",
    contentSelector: ".article-entry",
    headingSelector: "h1, h2, h3, h4, h5, h6",
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: "main",
    positionFixedSelector: ".tocbot",
    positionFixedClass: "is-position-fixed",
    fixedSidebarOffset: "auto",
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link
  rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css"
/>
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->
 <!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script> 
<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->

<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->
 
<link rel="stylesheet" href="/css/clipboard.css">
 <script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>
 
<!-- CanvasBackground -->

<script>
  if (window.mermaid) {
    mermaid.initialize({ theme: "forest" });
  }
</script>


    
    

  </div>
</body>

</html>